<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 把 接收多个参数的函数转换为接受一个单一参数的函数

        // 非柯里化
        var add = function(x,y){
            return x + y
        }
        add(3,4)

        // 柯里化
        var add2 = function (x){
            return function(y){
                return x + y
            }
        }
        add2(3)(4)

        const curry = function(fn){
            return function curriedFn(...args){
                console.log('args',args); //[1],[1,2]
                console.log('fn',fn.length) // 传一个值就是1，传两个值就是2
                if(args.length<fn.length){
                    return function(){
                        console.log('...arguments',...arguments)
                        return curriedFn(...args.concat([...arguments]))
                    }
                }
                return fn(...args)
            }
        }
        const fn = (x,y,z,a)=>x+y+z+a
        const myfn = curry(fn)
        console.log(myfn(1)(2)(3)(1));
        
    </script>
</body>
</html>